/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.type;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import alloy.api.Atom;
import alloy.ast.Command;
import alloy.util.Dbg;
import alloy.util.ObjID;

/**
 * A type with a scope ({@link SigType} and {@link IntType} for now).
 */

public abstract class ScopedType extends ObjID {

    /** <code>true</code> if the scope of this
        has been set by a call to {@link #setScope}
    */
    private boolean _scopeSet;

    /** the scope of this */
    private int _scope;

    /** atoms that represent this type in an instance */
    private Atom[] _atoms;

    /** set the scope of this */
    public void setScope(int scope_) {
        _scope = scope_;
        _scopeSet = true;
        _atoms = new Atom[scope_];
        for (int i = 0; i < scope_; i++) {
            _atoms[i] = new Atom();
        }
    }

    /** get the scope of this.  <em>NOTE:</em> should only
        be called after the scope of this has been set with
        {@link #setScope}.
    */
    public int getScope() {
        alloy.util.Dbg.chk(_scopeSet, toString());
        return _scope;
    }

    /** @return <code>true</code> if the scope of this has been set by
        {@link #setScope}, <code>false</code> otherwise */
    public boolean scopeSet() {
        return _scopeSet;
    }

    /** get the ith atom in this type, numbered from zero.  <em>NOTE:</em> should only
        be called after the scope of this has been set with
        {@link #setScope}.
    */
    public Atom getAtom(int i) {
        Dbg.chk(scopeSet(), "SigType.getAtom");
        return _atoms[i];
    }

    /**
     * returns the atoms array as a List, so that it can be stored for instance
     * specific purposes
     *
     */
    public List getAtomList() {
        return Arrays.asList(_atoms);
    }

    public Atom[] getAtoms() {
        return _atoms;
    }

    /**
     * Set the scopes of all "true" {@link BasicType}s for a given
     * command.
     * @see alloy.AlloyRunner#_analyzeCommand
     */
    public static void setBasicTypeScopes(Command command_) {
        Map leafId2Scope = command_.leafId2Scope;
        Map stringToType = command_.stringToType;
        //System.out.println("setBasicTypeScopes: stringToType " + stringToType);
        // do sig types first
        Iterator sigTypeIter = stringToType.values().iterator();
        while (sigTypeIter.hasNext()) {
            SigType sigType = (SigType)sigTypeIter.next();
            Integer scope = (Integer)leafId2Scope.get(sigType.getSignature().getLeafId());
            Dbg.chk(scope);
            sigType.setScope(scope.intValue());
        }
    }
}
